package org.chengyi.model_shop.controller;

import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.chengyi.model_shop.entity.User;
import org.chengyi.model_shop.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping
public class PersonController {

    @Autowired
    private PersonService personService;
    @GetMapping("/userInfo")
    public String getUserInfo()
    {
        return personService.getUserInfo();
    }
    @PutMapping("/updateUserInfo")
    public String updateUserInfo(@RequestBody User user, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Integer userId = (Integer) session.getAttribute("userId");

        if (userId == null) {
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", "用户未登录或会话已过期");
            return JSONUtil.toJsonStr(result);
        }

        user.setId(userId);
        return personService.updateUserInfo(user);
    }

}
